<template>
   
  <div>
    <el-upload
      :action="policy"
      :http-request="upload"
      list-type="picture"
      :multiple="false"
      :before-upload="beforeUpload"
    >
      <el-button size="small" type="primary" v-if="show">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传jpg/png文件，且不超过10MB
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="url" alt="" />
    </el-dialog>
  </div>
</template>
<script>
import { policy } from "./policy";
import http from '@/utils/httpRequest.js'
export default {
  name: "singleUpload",
  data() {
    return {
      show:true,
      dialogVisible: false,
      policy: "",
      url:"",
    };
  },
  methods: {
    close(){
      console.log("close")
      this.url = "",
      this.show = true
    },
    beforeUpload(file) {
      let _self = this;
      console.log(file);
      this.url = "http://192.168.2.80:10000/gulimall/"+file.name
      console.log(this.url)
      return new Promise((resolve, reject) => {
        policy(file.name)
          .then((response) => {
            this.policy = response;
            console.log(response);
            resolve(true);
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    upload(params) {
      const file = params.file;
      console.log(file);
      http({
        url: this.policy,
        data: file,
        method: "put",
        headers: {
          "Content-Type": "application/octet-stream",
        },
      });
      this.$emit('logoUrl', this.url)
      this.show = false
    },
  },
};
</script>
<style>
</style>


